package com.fjh.config;

import com.fjh.filter.SensitiveWordFilter;
import com.fjh.interceptor.LoginInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public SensitiveWordFilter sensitiveWordFilter() {
        return new SensitiveWordFilter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/activities/query-activities")
                .excludePathPatterns("/users/login", "/users/register");


        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/activities/add-activity")
                .excludePathPatterns("/users/login", "/users/register");
    }

    @Bean
    public FilterRegistrationBean<SensitiveWordFilter> sensitiveWordFilterRegistration() {
        // 注册过滤器，并指定应用于用户注册的路径
        FilterRegistrationBean<SensitiveWordFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(sensitiveWordFilter());
        registrationBean.addUrlPatterns("/*"); // 仅应用于用户注册
        registrationBean.setName("sensitiveWordFilterForRegistration");
        registrationBean.setOrder(1);
        return registrationBean;
    }
}
